package homework01;

import java.time.LocalDateTime;
import java.util.Scanner;

public class UserSystem {
    private User[] users = new User[100];
    private int usercount = 0;

    public void rigister(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入注册用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入注册密码:");
        String password = scanner.nextLine();

        if (!username.matches("\\w{1,16}")) {
            System.out.println("用户名不符合规则，注册失败！");
            return;
        }

        if (!password.matches(".*[A-Z].*")||!password.matches(".*[^a-zA-Z0-9].*")) {
            System.out.println("密码不符合规则，注册失败!");
            return;
        }

        for (int i = 0; i < usercount; i++) {
            if(users[i].getUsername().equals(username)){
                System.out.println("用户名已存在,注册失败");
                return;
            }
        }

        users[usercount++] = new User(username, password);
        System.out.println("注册成功");
    }

    public void login(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入登录用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入登录密码:");
        String password = scanner.nextLine();

        for (int i = 0; i < usercount; i++) {
            if (users[i].getUsername().equals(username) && users[i].getPassword().equals(password)) {
                users[i].setLogintime(LocalDateTime.now());
                System.out.println("登录成功!");
                return;
            }
        }
        System.out.println("用户名或密码错误,登录失败!");
    }
}
